<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <div>
    <el-select v-model="appletCategoryInfo"
               filterable
               clearable
               value-key="appletCategoryId"
               :placeholder="placeholder"
               @change="changeType">
      <el-option v-for="appletCategoryInfo in appletCategoryList" :key="appletCategoryInfo.appletCategoryId"
                 :label="appletCategoryInfo.categoryName"
                 :value="appletCategoryInfo"/>
    </el-select>
    <el-form label-width="30" v-if="appletCategoryInfo" style="margin-top: 18px">
      <el-form-item style="margin-bottom: -12px">
        <el-checkbox-group v-model="grantedApplets" style="line-height: 1px;">
          <el-checkbox border id="dashboard-checkbox" v-for="applet in appletCategoryInfo.appletVOS"
                       :key="applet.appletId"
                       :label="applet.appletId" :value="applet.appletId" @change="changeApplet(applet)">
            <span style="font-size: 12px;color: rgba(0,0,0,0.65);text-align: left;line-height: 20px;">
               {{applet.appletName}}
            </span>
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "DashboardAppletCategorySelect",
    props: ['value', 'placeholder', 'typeTags', 'applets','admin'],
    created() {
      this.loadAppletCategoryList()
    },
    data() {
      return {
        appletCategoryInfo: null,
        appletCategoryList: [],
        grantedApplets: this.applets
      }
    },
    methods: {
      changeType() {
        this.$emit('input', this.appletCategoryInfo.appletCategoryId)
        this.$emit('change', this.appletCategoryInfo)
      },
      changeApplet(applet) {
        var add = true
        if (this.grantedApplets.indexOf(applet.appletId) < 0) {
          add = false
        }
        this.$emit("returnApplet", {appletId: applet.appletId, routeUrl: applet.routeUrl, add: add});
      },
      // 获取应用分类及分类下的应用数据
      loadAppletCategoryList() {
        const url = '/joys-rest/joys-master-data/appletCategories'
        this.$http.get(url, {params: {typeTags: ['WIDGET'],admin:true}}).then(resp => {
          this.appletCategoryList = resp.data
          this.mapAppletCategoryInfo()
        }, resp => {
          this.$message.error(resp.data)
        })
      },
      mapAppletCategoryInfo() {
        this.appletCategoryList && this.appletCategoryList.find(appletCategoryInfo => {
          if (appletCategoryInfo.appletCategoryId == this.value) {
            this.appletCategoryInfo = appletCategoryInfo
            this.$emit('matched', this.appletCategoryInfo)
            return true
          } else {
            return false
          }
        })
      }
    },
    watch: {
      typeTags() {
        this.loadAppletCategoryList()
      },
      applets() {
        this.grantedApplets = this.applets
      }
    }
  }
</script>

<style scoped>
  #dashboard-checkbox {
    width: 148px;
    height: 22px;
    /*line-height: 22px;*/
    padding: 0;
    margin: 0 24px 12px 0;
    border-radius: 4px;
    border: 1px dashed #D9D9D9;
  }

  #dashboard-checkbox:hover {
    background: #F5F5F5;
    border: 1px solid #D9D9D9
  }

  #dashboard-checkbox >>> .el-checkbox__label {
    padding-left: 8px;
  }

  .checkbox-style {
    border: 1px dashed #d93e1a;
  }

  .is-checked {
    background: #F5F5F5;
    border: 1px solid #D9D9D9 !important;
  }

  #dashboard-checkbox >>> .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #2FC366;
    border-color: #2FC366;
  }

  #dashboard-checkbox >>> .el-checkbox__input {
    float: right;
    margin: 4px 3px 0 0;
  }

  #dashboard-checkbox >>> .el-checkbox__inner {
    border-radius: 100%;
    height: 10.5px;
    width: 10.5px;
    line-height: 12px;
    margin: 0;
    padding: 0;
    border-color: #D9D9D9;
  }

  #dashboard-checkbox >>> .el-checkbox__inner::after {
    height: 5px;
    width: 2.5px;
    margin-left: -1.2px;
    margin-top: -0.4px;
  }

</style>
